import Vue from 'vue';
import Vuex from 'vuex';
import {
	http
} from '@/utils/request';
Vue.use(Vuex);
const ACCESSTOKEN = uni.getStorageSync('accessToken') || '';
const REFERRER = uni.getStorageSync('referrer') || '';
const USERINFO = uni.getStorageSync('userInfo') || {};
const REFRESHTOKEN = uni.getStorageSync('refreshToken') || '';
const store = new Vuex.Store({
	state: {
		// 用户token
		accessToken: ACCESSTOKEN,
		// 用户信息
		userInfo: USERINFO,
		// 推荐人
		referrer: REFERRER,
		// 小程序openid
		openId: '',
		// 网络状态，用于下载提醒
		networkState: 'unknown',
		// 终止接下来的请求
		"location": {
			address: "",
			cityId: "",
			cityName: "",
			latitude: "",
			longitude: "",
			name: ""
		},
		requestLoading: true ,//加载等待是否显示:true
		isLoginPage:false, //是否是在登录页
	},
	getters: {
		// 全局配置
		themeColor: state => {
			let theme = state.themeColor;
			if (!theme) {
				// theme = $mConstDataConfig.themeList.filter((item) => item.name === ($mSettingConfig.styleType || 'rf'))[0];
			}
			return theme;
		},
		// 全局配置
		globalConfig: state => {
			return state.globalConfig;
		},
		// 获取网络状态
		networkStatus: state => {
			return state.networkState;
		},
		// 判断用户是否登录
		hasLogin: state => {
			return !!state.accessToken;
		}
	},
	mutations: {
		//显示请求加载动画
		request_show_loading(state) {
			state.requestLoading = true;
		},
		//隐藏请求加载动画
		request_hide_loading(state) {
			state.requestLoading = false;
		},
		async getUserInfo(state) {
			await http.get('/app/customer/customer/getInfo').then(res => {
				if (res && res.customer){
					state.userInfo = res.customer;
					uni.setStorageSync('userInfo', res.customer);
				}
			})
		},
		async login(state, token) {
			state.accessToken = token;
			state.refreshToken = token;
			uni.setStorageSync('accessToken', token);
			uni.setStorageSync('refreshToken', token);
			await http.get('/app/customer/customer/getInfo').then(res => {
				state.userInfo = res.customer;
				uni.setStorageSync('userInfo', res.customer);
				uni.switchTab({
					url: '/pages/s/index/index'
				})
				
				// let currentPage = getCurrentPages();
				// let _route = currentPage[currentPage.length - 2].route;
				// if (currentPage.length <= 1) {
				// 	uni.switchTab({
				// 		url: '/pages/s/index/index'
				// 	})
				// } else if (_route == 'pages/tabbar/my') {
				// 	uni.switchTab({
				// 		url: '/pages/tabbar/my'
				// 	})
				// } else {
				// 	uni.navigateBack({
				// 		delta: 1
				// 	});
				// }
			}).catch(e => {
				state.accessToken = '';
				state.refreshToken = '';
				uni.removeStorageSync('accessToken');
				uni.removeStorageSync('refreshToken');
			})
		},
		async logout(state, to = true) {
			await http.delete('/appNoAuth/logout').then(res => {
				state.accessToken = '';
				state.refreshToken = '';
				state.userInfo = {};
				uni.removeStorageSync('accessToken');
				uni.removeStorageSync('refreshToken');
				uni.removeStorageSync('userInfo');
				if (to) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
			})
		},
		getWxOpenid(state, options) {
			options.code = options.code || ''
			http.get('/appNoAuth/wxlogin', {
				code: options.code,
				encryptedData: '',
				iv: '',
			}).then(res => {
				if (res && res.msg){
					uni.setStorageSync('openid', res.msg);
				}
			})
		},
		setReferrer(state, referrer) {
			state.referrer = referrer;
			uni.setStorageSync('referrer', referrer);
		},
		setLocationState(state, info) {
			state.location = info;
			uni.setStorageSync('location', info);
		},
		setOpenId(state, openId) {
			state.openId = openId;
			uni.setStorageSync('openId', openId);
		},
		setNetworkState(state, provider) {
			state.networkState = provider;
		},
	},
	actions: {
		globalConfigChange({
			commit
		}, info) {
			commit('setGlobalConfig', info);
		},
		globalConfigChange({
			commit
		}, info) {
			commit('setGlobalConfig', info);
		},
		locationChange({
			commit
		}, info) {
			commit('setLocationState', info);
		},
		logout({
			commit
		}) {
			commit('logout');
		}
	}
});

export default store;
